package cn.tedu.pdfamily.product.controller;

import cn.tedu.pdfamily.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.pdfamily.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.pdfamily.product.pojo.vo.CategoryListItemVO;
import cn.tedu.pdfamily.product.pojo.vo.CategoryStandardVO;
import cn.tedu.pdfamily.product.service.ICategoryService;
import cn.tedu.pdfamily.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理分类相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/categories")
@Api(tags = "02. 分类管理模块")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;

    public CategoryController() {
        log.debug("创建控制器对象:CategoryController");
    }

    // http://localhost:9080/categorys/add-new
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("开始处理【添加分类】的请求，参数：{}", categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        log.debug("添加分类成功！");
        return JsonResult.ok();
    }

    // http://localhost:9080/categorys/9527/delete
    @ApiOperation("根据id删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除分类失败，尝试删除的分类的ID无效！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除分类】的请求，参数：{}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categorys/9527/update
    @ApiOperation("修改分类详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, CategoryUpdateDTO categoryUpdateDTO) {
        log.debug("开始处理【修改分类详情】的请求，参数ID：{}, 新数据：{}", id, categoryUpdateDTO);
        categoryService.updateInfoById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/categorys/9527
    @ApiOperation("根据id查询分类详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询分类详情】的请求，参数：{}", id);
        CategoryStandardVO category = categoryService.getStandardById(id);
        return JsonResult.ok(category);
    }

    // http://localhost:9080/categorys
    @ApiOperation("查询分类列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list() {
        log.debug("开始处理【查询分类列表】的请求，无参数");
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }
}
